﻿using System;

namespace LpbPrj.Client.Tools.Helper
{
    /// <summary>
    /// 计算日期的间隔(静态类)
    /// </summary>
    public static class DateTimeDiff
    {
        /// <summary>
        /// 计算日期间隔
        /// </summary>
        /// <param name="d1">要参与计算的其中一个日期</param>
        /// <param name="d2">要参与计算的另一个日期</param>
        /// <param name="drf">决定返回值形式的枚举</param>
        /// <returns>一个代表年月日的int数组，具体数组长度与枚举参数drf有关</returns>
        public static int[] toResult(DateTime d1, DateTime d2, diffResultFormat drf)
        {
            #region 数据初始化
            //DateTime max;
            //DateTime min;
            //int year;
            //int month;
            //int tempYear, tempMonth;
            //if (d1 > d2)
            //{
            //    max = d1;
            //    min = d2;
            //}
            //else
            //{
            //    max = d2;
            //    min = d1;
            //}
            //tempYear = max.Year;
            //tempMonth = max.Month;
            //if (max.Month < min.Month)
            //{
            //    tempYear--;
            //    tempMonth = tempMonth + 12;
            //}
            //year = tempYear - min.Year;
            //month = tempMonth - min.Month;
            DateTime max;
            DateTime min;
            int year;
            int month;
            int day;
            if (d1 > d2)
            {
                max = d1;
                min = d2;
            }
            else
            {
                max = d2;
                min = d1;
            }
            year = max.Year - min.Year;
            month = max.Month - min.Month;
            day = max.Day - min.Day;
            #endregion
            #region 按条件计算
            if (drf == diffResultFormat.dd)
            {
                TimeSpan ts = max - min;
                return new int[] { ts.Days };
            }
            if (drf == diffResultFormat.mm)
            {
                return new int[] { month + year * 12 + (day < 0 ? -1 : 0) };
            }
            if (drf == diffResultFormat.yy)
            {
                return new int[] { year };
            }
            return new int[] { year, month };
            #endregion
        }

        /// <summary>
        /// 计算日期间隔
        /// </summary>
        /// <param name="d1">要参与计算的其中一个日期</param>
        /// <param name="d2">要参与计算的另一个日期</param>
        /// <param name="drf">决定返回值形式的枚举</param>
        /// <returns>一个代表年月日的int数组，具体数组长度与枚举参数drf有关</returns>
        public static string toResult(DateTime d1, DateTime d2, string format = "{0}岁{1}个月")
        {
            #region 数据初始化
            DateTime max;
            DateTime min;
            int year;
            int month;
            int tempYear, tempMonth;
            if (d1 > d2)
            {
                max = d1;
                min = d2;
            }
            else
            {
                max = d2;
                min = d1;
            }
            tempYear = max.Year;
            tempMonth = max.Month;
            if (max.Month < min.Month)
            {
                tempYear--;
                tempMonth = tempMonth + 12;
            }
            year = tempYear - min.Year;
            month = tempMonth - min.Month;
            #endregion

            return string.Format(format, year, month);
        }
    }
    /// <summary>
    /// 关于返回值形式的枚举
    /// </summary>
    public enum diffResultFormat
    {
        /// <summary>
        /// 年数和月数
        /// </summary>
        yymm,
        /// <summary>
        /// 年数
        /// </summary>
        yy,
        /// <summary>
        /// 月数
        /// </summary>
        mm,
        /// <summary>
        /// 天数
        /// </summary>
        dd,
    }
}
